package com.baizhi.bzmall.admin.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectResult;
import com.baizhi.bzmall.admin.service.IBzBrandService;
import com.baizhi.bzmall.common.vo.ResultEntity;
import com.baizhi.bzmall.entity.BzBrand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/bzBrand")
public class BzBrandController {
    @Autowired
    private IBzBrandService bzBrandService;
    @Value("${OSS.endpoint}")
    private String endpoint;
    @Autowired
    private OSS ossClient;
    @GetMapping
    public ResultEntity selectAll(){
        List<BzBrand> list = bzBrandService.list();
        return ResultEntity.ok(list);
    }
    @PostMapping
    public ResultEntity add(@RequestBody BzBrand brand){
        boolean save = bzBrandService.save(brand);
        if(save){
            return ResultEntity.ok();
        }else{
            return ResultEntity.error("插入失败");
        }
    }
    @PostMapping("/upload")
    public ResultEntity upload(MultipartFile mf) throws IOException {
        String bulkName ="bz-mall-brand";
        String key= UUID.randomUUID().toString()+mf.getOriginalFilename(); //文件的唯一标识，通常是不重复的文件名
        InputStream in = mf.getInputStream();

        PutObjectResult putObjectResult = ossClient.putObject(bulkName, key, in);
        String result="https://"+bulkName+"."+endpoint+"/"+key;
        return ResultEntity.ok(result);
    }
}

